﻿using BatchPrint.Converter;
using BatchPrint.Models;
using Microsoft.Extensions.DependencyInjection;

namespace BatchPrint.Services;

public class ConverterProvider
{
    private readonly IServiceProvider _serviceProvider;
    private readonly Dictionary<FileType, List<string>> _dict;

    public ConverterProvider(IServiceProvider provider)
    {
        _serviceProvider = provider;
        _dict = new Dictionary<FileType, List<string>>()
        {
            { FileType.Image, [".jpg", ".jpeg", ".png"] },
            { FileType.Excel, [".xls", ".xlsx"] },
            { FileType.Pdf, [".pdf"] },
            { FileType.Word, [".doc", ".docx"] }
        };
    }

    public FileType GetFileType(string filePath)
    {
        var ext = filePath.Substring(filePath.LastIndexOf(".")).ToLower();
        foreach (var item in _dict)
        {
            if (item.Value.Contains(ext))
                return item.Key;
        }
        return FileType.Unknow;
    }

    public IImageConverter GetConverter(string filePath)
    {
        var fileType = GetFileType(filePath);
        return _serviceProvider.GetRequiredKeyedService<IImageConverter>(fileType);
    }
}
